#include "stdafx.h"
#include "Resource.h"
#include "TGRx.h"
#include "TGRxImplClass.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define TGRX_BUILD_TYPE		_T("1")
#define TGRX_CODEPAGE		_T("804")

TGRxDictionary* TGRxApp::rxSysRegistry()
{
	if (TGRxGlobal::m_pGlobal == NULL)
		return NULL;

	return TGRxGlobal::m_pGlobal->m_pRxDictionary;
}

TGRxClass* TGRxApp::newTGRxClass(const char* className,
						const char* parentClassName,
						int         mdlVer,
						int         classVer,
						int			nProxyFlags,
						TGRxObject* (*pseudoConstructor)(),
						const char* appName)
{
	TGRxDictionary * pRxDictionary = rxSysRegistry();
	ASSERT(pRxDictionary != NULL);

	pRxDictionary = (TGRxDictionary *)pRxDictionary->at(TGRX_CLASS_DICTIONARY);
	
	TGRxImplClass * pImplClass = NULL;
	TGRxObject * pRxObject = pRxDictionary->at(className);
	if (pRxObject == NULL)
	{
		pImplClass = new TGRxImplClass;
		pImplClass->setClassName(className);
		pImplClass->setParentClassName(parentClassName);
		pImplClass->setMDLVer(mdlVer);
		pImplClass->setClassVer(classVer);
		pImplClass->setMakeClassObjFunc(pseudoConstructor);
		pImplClass->setAppName(appName);
		pImplClass->setProxyFlags(nProxyFlags);
		pRxDictionary->atPut(className,(TGRxObject *)pImplClass);
		return (TGRxClass*)pImplClass;
	}
	else
	{
		//already reg
		ASSERT(FALSE);
	}
	return pImplClass;
}

void TGRxApp::deleteTGRxClass(TGRxClass * pRxClass)
{
	TGRxDictionary * pRxDict = rxSysRegistry();
	if (pRxDict != NULL)
	{
		TGRxDictionary * pRxDictClass = (TGRxDictionary *)pRxDict->at(TGRX_CLASS_DICTIONARY);
		if (pRxDictClass != NULL)
		{
			TGRxObject * pRxObject = pRxDictClass->remove(pRxClass->name());
			if (pRxObject != NULL)
				delete pRxObject;
		}
	}
}

CString TGRxApp::GetTGProductKey()
{
	return TGRX_COMPANY _T("\\") TGRX_APPNAME _T("\\") TGRX_VERSION _T("\\")
				TGRX_APPNAME _T("-") TGRX_BUILD_TYPE _T(":") TGRX_CODEPAGE;
}

TG::ErrorStatus TGRxApp::SetTGLastError(TG::ErrorStatus es)
{
	if (TGRxGlobal::m_pGlobal != NULL)
	{
		TGRxGlobal::m_pGlobal->m_eLastError = es;
		return es;
	}

	return TG::eNoTGGlobal;
}

TG::ErrorStatus TGRxApp::GetTGLastError()
{
	if (TGRxGlobal::m_pGlobal != NULL)
	{
		return TGRxGlobal::m_pGlobal->m_eLastError;
	}

	return TG::eNoTGGlobal;
}

#define BEGIN_ERROR_STATUS_MAP(eErrorStatus)	\
	switch (eErrorStatus)	\
	{
#define ON_ERROR_STATUS_INFO(ErrorID, nInfoID)	\
	case ErrorID:	\
		::LoadString(theResourceHandle, nInfoID, sInfo.GetBuffer(MAX_PATH), MAX_PATH);	\
		sInfo.ReleaseBuffer();	\
		break;
#define END_ERROR_STATUS_MAP()	\
	default:	\
		::LoadString(theResourceHandle, IDS_ES_UNKNOWN, sInfo.GetBuffer(MAX_PATH), MAX_PATH);	\
		sInfo.ReleaseBuffer();	\
		break;	\
	}

void TGRxApp::GETTGErrorDescript(TG::ErrorStatus es, CString& sInfo)
{
	BEGIN_ERROR_STATUS_MAP(es)
		ON_ERROR_STATUS_INFO(TG::eOk, IDS_ES_OK)
		ON_ERROR_STATUS_INFO(TG::eMrxNotLoaded, IDS_ES_MRXNOTLOADED)
		ON_ERROR_STATUS_INFO(TG::eMrxHasLoaded, IDS_ES_MRXHASLOADED)
		ON_ERROR_STATUS_INFO(TG::eMrxHasLocked, IDS_ES_MRXHASLOCKED)
		ON_ERROR_STATUS_INFO(TG::eMrxInitError, IDS_ES_MRXINITERROR)
		ON_ERROR_STATUS_INFO(TG::eFileNotFound, IDS_ES_FILENOTFOUND)
		ON_ERROR_STATUS_INFO(TG::eFileAccessErr, IDS_ES_FILEACCESSERR)
		ON_ERROR_STATUS_INFO(TG::eEntryNotFound, IDS_ES_ENTRYNOTFOUND)
		ON_ERROR_STATUS_INFO(TG::eInvalidVersion, IDS_ES_INVALIDVERSION)
		ON_ERROR_STATUS_INFO(TG::eOutOfMemory, IDS_ES_OUTOFMEMORY)
	END_ERROR_STATUS_MAP()
}
